/* ------------------------------------------------------------------------------
   Originally coded by completely dead Lego @ 09/05/2011
------------------------------------------------------------------------------ */ 
#include <iostream>
#include <conio.h>				// my old-school codin' support
#include <string>
#include <stdio.h>
#define X_TESTS			300		// cycles for test (recommended: 150-450)
#define X_SHIFT			5		// data increment
#define SIMPLE_INS		0		// another way to binary tree
#define LIST_MAXSIZE	2048	// max size of the list [fixed und the limited]
#define NO_ELEMENTS		"No elements found."
#define DEBUG_MODE		0		// 0 - off, 1 - print and pause, 2 - just print
#define DBG_MAXLEV		64		// limit fo printin
#define DBG_LIMIT		128		// debug_mode must be enabled for this
#include "cpp_fix.h"			// like-a-c functions support
#include "init.h"				// initialization
#include "bs_tree.cpp"
#include "tests.cpp"

using namespace std;
using tst::main_test;

/* --------------------------------------
   main is so main
-------------------------------------- */
int main(int argc, char **argv) {
	bs_tree tree, tree2; // = new bs_tree;
	int t,r=1;
	bs_tree n;
	randomize();

	if( argc >= 2 && argv[1] == string("-test") ) {
		main_test();
		_getch();
		return 0xAA;
	}

	for( int i=0; i<10; i++ )
		tree.insert( rand()%10 );

	while( r ) {
		clrscr();

		tree.debug_print(1);

		gotoxy( 1, 16 );

		cout << "\r\n DFS_DEBUG: ";
		tree.debug_dfs();
		cout << " | LIST_DEBUG: ";
		tree.debug_list();
		cout << "\n";
		if( tree.err != "" )
			cout << "\n Error: " << tree.err;
		cout << "\n------------------------------------------------ Build: " << __DATE__ << ' ' << __TIME__ << " --\n"
			 <<"(i)ns (m)ax (d)elmin (c)ur (p)rev (f)ind (e)xcl (s)plit s(u)bst (t)est (q)uit\n";
		
		switch( _getch() ) {
			case 27:
				r = 0;
				break;
			case 'q':
				exit(0xEA);
			case 'i':
				cout << "\nType some integer value: ";
				scanf_s( "%d", &t );
				tree.insert( t );
				break;
			case 'd':
				tree.deletemin();
				break;
			case 'm':
				cout << "\n\n MAX: " << tree.max() << ' ' << tree.err;
				_getch();
				break;
			case 'c':
				cout << "\n\n CURRENT: " << tree.current() << ' ' << tree.err;
				_getch();
				break;
			case 'p':
				cout << "\n\n PREV: " << tree.prev() << ' ' << tree.err;
				_getch();
				break;
			case 'r':
				cout << "what do you want to remove?\n";
				scanf_s( "%d", &t );
				tree.remove( t );
				break;
			case 'b':
				tree = tree2.copy();
				break;
			case 'f':
				cout << "what do you want to find?\n";
				scanf_s( "%d", &t );
				if( ( t = tree.find( t ) ) != -1 )
					cout << " - element found at position " << t;
				else
					cout << " - element NOT found";
				_getch();
				break;
			case 's':
				cout << "type some value for split: ";
				scanf_s( "%d", &t );
				tree2 = tree.split( t );
				tree2.debug_list();
				_getch();
				break;
			case 'u':
				do {
					cout << "type position between 0 and " << tree.count() << ": ";
					scanf_s( "%d", &t );
				}
				while( t < 0 || t > tree.count() );
				tree.subst( enter_list(), t );
				break;
			case 'e':
				tree.excl( enter_list() );
				break;
			case 't':
				cout << "1 - 1000000 cycles of create-insert_5-destroy\n2 - time test\n3 - t(N) test\nAnyKey - back to menu\n";
				switch( _getch() ) {
					case '1':
						cout << "Test 1... ";
						for( long i=0; i<1000000; i++ )
							tst::test_1();
						cout << "OK";
						_getch();
						break;
					case '2':
						cout << "Test 2...\n";
						cout << "data count: ";
						scanf_s( "%d", &t );
						cout << t << ' ' << tst::test_2(t) << "\nOK\n";
						_getch();
						break;
					case '3':
						clrscr();
						main_test();
						_getch();
						break;

				}
				break;
		}
	}

	return 0xFF;
}
